package com.study.javaOther.bishi;

/**
 * @ClassName: MainDemo
 * @Description: 万德笔试：字母和数字的转换
 * @author: 王胜
 * @date 2021/6/18
 * @Version: 1.0
 **/
public class MainDemo {
    private final static String[] LETTER = {
            "A", "B", "C", "D", "E", "F", "G",
            "H", "J", "K", "L", "M", "N",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};

    public static String numConvertString(int num) {
        String s = "";

        if (num == 0) {
            return " ";
        }
        if (num == 1) {
            return "A";
        }
        num -= 1;
        boolean flag = true;
        //商==0 代表循环完毕
        while (num != 0) {
            int i = num % 24;
            String c;
            if (flag)
                c = LETTER[i];
            else
                c = LETTER[i - 1];
            s = c + s;
            //求商
            num = num / 24;
            flag = false;
        }
        //返回结果
        return s;
    }

    public static Integer stringConvertNum(String numStr) throws Exception {
        if (numStr.isEmpty()) {
            return -1;
        }
        int index = 0;
        int num = 0;
        while (numStr.length() >= index + 1) {
            num = num * 24;
            String substring = numStr.substring(index, index + 1);
            int i = getIndex(substring);
            if (i == -1) {
                throw new Exception("你输入的字符串不正确");
            }
            num += i;
            index++;
        }
        return num;
    }

    public static int getIndex(String s) {
        for (int i = 1; i <= LETTER.length; i++) {
            if (LETTER[i - 1].equals(s)) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) throws Exception {
        int index = 1;
        for (int i = 1; i < 50; i++) {
            System.out.print(i + ":" + numConvertString(i) + "\t" + "\t" + stringConvertNum(numConvertString(i)) + "\t");
            if (index >= 24) {
                index = 0;
                System.out.println();
            }
            index++;
        }
    }
}
